import {Component} from "react";
import Taro from "@tarojs/taro";
import LogService from "../service/LogService";


class BaseComponent extends Component {

  tag = "BaseComponent";

  getTag(){
    return this.tag;
  }

  // onLoad
  onLoad (option) {
    this.printLifeCycle("onLoad");
  }

  // onReady 生命周期函数--监听页面初次渲染完成
  onReady () {
    this.printLifeCycle("onReady");
  }

  // onUnload 生命周期函数--监听页面卸载
  onUnload (){
    this.printLifeCycle("onUnload");
  }

  componentWillMount (){
    // this.printLifeCycle("componentWillMount");
  }

  componentDidMount(){
    this.printLifeCycle("componentDidMount");
  }

  componentWillUnmount(){
    this.printLifeCycle("componentWillUnmount");
  }

  // 对应 onShow
  componentDidShow () {
    this.printLifeCycle("componentDidShow");
  }

  // 对应 onHide
  componentDidHide () {
    this.printLifeCycle("componentDidHide");
  }

  printLifeCycle(text){
    if (this.getTag() == "BaseComponent")
      return;
    LogService.debug(this.getTag(), text);
  }

  // 对应 onPullDownRefresh，除了 componentDidShow/componentDidHide 之外，
  // 所有页面生命周期函数名都与小程序相对应
  onPullDownRefresh () {}

  /**
   * @param url
   * @param events
   */
  navigateTo (url, events){
    Taro.navigateTo({
      url,
      events,
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        // res.eventChannel.emit('acceptDataFromOpenerPage', { data: 'test' })
      }
    })
  }

  /**
   * @param url
   * @param events
   */
  redirectTo (url){

    // Taro.getCurrentPages().pop();
    // this.navigateTo(url);
    Taro.redirectTo({
      url,
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        // res.eventChannel.emit('acceptDataFromOpenerPage', { data: 'test' })
      }
    })
  }
}

export default BaseComponent;
